package com.iemes.controller.device;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.DevOperationFormMap;
import com.iemes.entity.DeviceFaultDetailMap;
import com.iemes.entity.DevicePartsMap;
import com.iemes.entity.DeviceTypeFormMap;
import com.iemes.mapper.DeviceMapper;
import com.iemes.mapper.UserMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/devicetype/")
public class DeviceTypeController extends BaseController {

    @Inject
    private UserMapper userMapper;

    @Inject
    private DeviceMapper deviceMapper;

    @RequestMapping("devicetype")
    public String listUI(Model model) throws Exception {
        String [] btnArray = new String[]{"<button type=\"button\" id=\"addAccount\" class=\"btn btn-primary marR10\">新增</button>","<button type=\"button\" id=\"editAccount\" class=\"btn btn-info marR10\">编辑</button>","<button type=\"button\" id=\"delAccount\" class=\"btn btn-danger marR10\">删除</button>"};
        model.addAttribute("res", findByRes());
        model.addAttribute("btn",btnArray);
        return Common.BACKGROUND_PATH + "/device/type/devicetype_list";
    }

    @ResponseBody
    @RequestMapping("devicetype_findByPage")
    public PageInfo findByPage(String pageNow,
                               String pageSize, HttpServletRequest request) throws Exception {

        //采用pageinfo分页
        if(pageNow == null){
            PageHelper.startPage(1, 10);
        }else {
            PageHelper.startPage(Integer.parseInt(pageNow), 10);
        }
//        String name =  getPara("deviceName");

        DeviceTypeFormMap deviceTypeFormMap=getFormMap(DeviceTypeFormMap.class);
//        toFormMap(deviceTypeFormMap,pageNow,pageSize,deviceTypeFormMap.getStr("orderby"));
        String name=request.getParameter("deviceTypeName");

        if(name==null || name.equals("")){
            List<DeviceTypeFormMap> deviceTypeFormMaps=deviceMapper.getDeviceType();
            PageInfo pageInfo = new PageInfo<>(deviceTypeFormMaps);
            return pageInfo;

        }else{
            List<DeviceTypeFormMap> deviceTypeFormMaps=deviceMapper.getTypeByName(name);
            PageInfo pageInfo = new PageInfo<>(deviceTypeFormMaps);
            return pageInfo;

        }

    }


    @RequestMapping("devicetype_addUI")
    public String addUI(Model model) throws Exception {
        model.addAttribute("list",deviceMapper.getClient());
        return Common.BACKGROUND_PATH + "/device/type/devicetype_add";
    }




    @ResponseBody
    @RequestMapping("devicetype_addEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
    @SystemLog(module="设备种类管理",methods="设备种类新增")//凡需要处理业务逻辑的.都需要记录操作日志
    public String addEntity(){
        DeviceTypeFormMap deviceTypeFormMap=getFormMap(DeviceTypeFormMap.class);
        try {
            userMapper.addEntity(deviceTypeFormMap);//新增后返回新增信息
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success";
    }


    @RequestMapping("devicetype_editUI")
    public String editUI(Model model, HttpServletRequest request) throws Exception {
        //获取供应商
        model.addAttribute("list",deviceMapper.getClient());

        String id = getPara("id");
        if(Common.isNotEmpty(id)){
            model.addAttribute("user",userMapper.findbyFrist("deviceTypeId", id, DeviceTypeFormMap.class));
        }
        return Common.BACKGROUND_PATH + "/device/type/devicetype_edit";
    }


    @ResponseBody
    @RequestMapping("devicetype_editEntity")
    @SystemLog(module="设备种类管理",methods="设备种类编辑")//凡需要处理业务逻辑的.都需要记录操作日志
    public String editEntity(String txtGroupsSelect) throws Exception {

        DeviceTypeFormMap deviceTypeFormMap=getFormMap(DeviceTypeFormMap.class);
        deviceTypeFormMap.put("txtGroupsSelect", txtGroupsSelect);
        userMapper.deleteByAttribute("deviceTypeId", deviceTypeFormMap.get("deviceTypeId")+"", DeviceTypeFormMap.class);
        userMapper.addEntity(deviceTypeFormMap);
        return "success";
    }



    @ResponseBody
    @RequestMapping("devicetype_deleteEntity")
    @Transactional(readOnly=false)//需要事务操作必须加入此注解
    @SystemLog(module="设备种类管理",methods="删除设备种类")//凡需要处理业务逻辑的.都需要记录操作日志
    public String delDeviceType() throws Exception {
        String[] ids = getParaValues("ids");
        for (String id : ids) {
            userMapper.deleteByAttribute("deviceTypeId", id, DeviceTypeFormMap.class);
        }
        return "success";
    }



    /**
     * 保养部位维护
     * @param model
     * @return
     */
    @RequestMapping("typePart")
    public String typePart(Model model){
        //获取相关的项目信息然之后分割字符串之后根据检验内容进行查询

        String deviceTypeId = getPara("id");
        //根据获取设备类别名称
        model.addAttribute("user",userMapper.findbyFrist("deviceTypeId", deviceTypeId, DeviceTypeFormMap.class));

        //获取设备关联的部位
        List<DevicePartsMap> ListParts = deviceMapper.getPartsByTypeId(deviceTypeId);
        System.out.println("listDetail:.."+ListParts);

        model.addAttribute("list",ListParts);
        return Common.BACKGROUND_PATH + "/device/type/devicetype_part";
    }

    @ResponseBody
    @RequestMapping("devicetype_editpart")
    @Transactional//需要事务操作必须加入此注解
    @SystemLog(module="设备种类管理",methods="设备种类编辑")//凡需要处理业务逻辑的.都需要记录操作日志
    public String editpart(String txtGroupsSelect,HttpServletRequest request) throws Exception {

        String deviceTypeId = request.getParameter("deviceTypeId");
        String deviceTypeName = request.getParameter("deviceTypeName");

        String[] itemNoArr = request.getParameterValues("ly_dev_part");

        userMapper.deleteByAttribute("deviceTypeId", deviceTypeId, DevicePartsMap.class);

        if(null != itemNoArr ){
            for(int i = 0 ;i<itemNoArr.length;i++){
                DevicePartsMap devicePartsMap = new DevicePartsMap();
                devicePartsMap.put("deviceTypeId", deviceTypeId);
                devicePartsMap.put("deviceTypeName", deviceTypeName);
                devicePartsMap.put("devMaintenancePartName", itemNoArr[i]);
                baseMapper.addEntity(devicePartsMap);
            }
        }

        return "success";
    }
}
